package com.yoga.manager.app.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.PlainVipBO;
import com.yoga.dao.dao.PlainVipDao;
import com.yoga.dao.domain.PlainVipDO;
import com.yoga.manager.app.AppPlainVipManager;
import com.yoga.manager.beanUtil.PlainVipBeanUtil;
import com.yoga.manager.uitl.DateStyle;
import com.yoga.manager.uitl.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by hdx on 2020/9/26.
 */
@Component
public class AppPlainVipManagerImpl implements AppPlainVipManager {
    Logger logger = LoggerFactory.getLogger(AppPlainVipManagerImpl.class);

    @Resource
    private PlainVipDao plainVipDao;


    @Override
    public PlainVipBO getPlainVipByUserId(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        PlainVipDO plainVipDO = plainVipDao.findByYogaUserId(yogaUserId);
        if (plainVipDO == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_PLAIN_NULL);
        }
        Date nowDate = DateUtil.dateFormat(new Date(), DateStyle.YYYY_MM_DD);
        PlainVipBO plainVipBO = PlainVipBeanUtil.plainVipDOToBO(plainVipDao.findById(plainVipDO.getId()));
        if (plainVipBO.getVipType() == 0) {
            plainVipBO.setVipStatus(0);
        } else if (plainVipBO.getVipType() == 1) {
            if (plainVipBO.getVipStartDate().getTime() <= nowDate.getTime() && plainVipBO.getVipEndDate().getTime() >= nowDate.getTime() && plainVipBO.getStopEndDate().getTime() <= nowDate.getTime()) {
                plainVipBO.setVipStatus(1);//会员
            } else if (plainVipBO.getVipEndDate().getTime() < nowDate.getTime() && plainVipBO.getStopEndDate().getTime() <= nowDate.getTime()) {
                plainVipBO.setVipStatus(2);//到期
            } else if (plainVipBO.getStopEndDate().getTime() > nowDate.getTime()){
                plainVipBO.setVipStatus(3);//停卡
            }
        } else if (plainVipBO.getVipType() == 2) {
            if (plainVipBO.getClassNum() > 0) {
                plainVipBO.setVipStatus(1);
            } else {
                plainVipBO.setVipStatus(2);
            }
        }

        return plainVipBO;
    }

    @Override
    public Boolean getIsPlainVIP(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        PlainVipBO plainVipBO = getPlainVipByUserId(yogaUserId);
        if (plainVipBO == null) {
            return false;
        }
        if (plainVipBO.getVipStatus() != 1) {
            return false;
        }

        return Boolean.TRUE;
    }
}
